home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 16 windows forms / otherobjects / cursorform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  8.0 KB  |  208 lines

  1. Public Class CursorForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         AddHandler Application.Idle, AddressOf Application_Idle
  10.  
  11.         'This call is required by the Windows Form Designer.
  12.         InitializeComponent()
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.  
  19.         RemoveHandler Application.Idle, AddressOf Application_Idle
  20.  
  21.  
  22.         If disposing Then
  23.             If Not (components Is Nothing) Then
  24.                 components.Dispose()
  25.             End If
  26.         End If
  27.         MyBase.Dispose(disposing)
  28.     End Sub
  29.     Friend WithEvents lblStatus As System.Windows.Forms.Label
  30.     Friend WithEvents btnMoveCursor As System.Windows.Forms.Button
  31.     Friend WithEvents chkClipCursor As System.Windows.Forms.CheckBox
  32.     Friend WithEvents btnSetCursor As System.Windows.Forms.Button
  33.     Friend WithEvents btnHide As System.Windows.Forms.Button
  34.     Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
  35.     Friend WithEvents btnLoad As System.Windows.Forms.Button
  36.     Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
  37.  
  38.     'Required by the Windows Form Designer
  39.     Private components As System.ComponentModel.Container
  40.  
  41.     'NOTE: The following procedure is required by the Windows Form Designer
  42.     'It can be modified using the Windows Form Designer.  
  43.     'Do not modify it using the code editor.
  44.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  45.         Me.btnSetCursor = New System.Windows.Forms.Button()
  46.         Me.chkClipCursor = New System.Windows.Forms.CheckBox()
  47.         Me.lblStatus = New System.Windows.Forms.Label()
  48.         Me.btnMoveCursor = New System.Windows.Forms.Button()
  49.         Me.PictureBox1 = New System.Windows.Forms.PictureBox()
  50.         Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
  51.         Me.btnLoad = New System.Windows.Forms.Button()
  52.         Me.btnHide = New System.Windows.Forms.Button()
  53.         Me.SuspendLayout()
  54.         '
  55.         'btnSetCursor
  56.         '
  57.         Me.btnSetCursor.Location = New System.Drawing.Point(16, 72)
  58.         Me.btnSetCursor.Name = "btnSetCursor"
  59.         Me.btnSetCursor.Size = New System.Drawing.Size(120, 48)
  60.         Me.btnSetCursor.TabIndex = 4
  61.         Me.btnSetCursor.Text = "Show Hourglass"
  62.         '
  63.         'chkClipCursor
  64.         '
  65.         Me.chkClipCursor.BackColor = System.Drawing.SystemColors.ControlDark
  66.         Me.chkClipCursor.Location = New System.Drawing.Point(160, 16)
  67.         Me.chkClipCursor.Name = "chkClipCursor"
  68.         Me.chkClipCursor.Size = New System.Drawing.Size(256, 80)
  69.         Me.chkClipCursor.TabIndex = 2
  70.         Me.chkClipCursor.Text = "Clip the mouse cursor to this area"
  71.         Me.chkClipCursor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
  72.         '
  73.         'lblStatus
  74.         '
  75.         Me.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  76.         Me.lblStatus.Dock = System.Windows.Forms.DockStyle.Bottom
  77.         Me.lblStatus.Location = New System.Drawing.Point(0, 245)
  78.         Me.lblStatus.Name = "lblStatus"
  79.         Me.lblStatus.Size = New System.Drawing.Size(480, 24)
  80.         Me.lblStatus.TabIndex = 0
  81.         '
  82.         'btnMoveCursor
  83.         '
  84.         Me.btnMoveCursor.Location = New System.Drawing.Point(16, 16)
  85.         Me.btnMoveCursor.Name = "btnMoveCursor"
  86.         Me.btnMoveCursor.Size = New System.Drawing.Size(120, 48)
  87.         Me.btnMoveCursor.TabIndex = 1
  88.         Me.btnMoveCursor.Text = "Move cursor to center of form"
  89.         '
  90.         'PictureBox1
  91.         '
  92.         Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  93.         Me.PictureBox1.Location = New System.Drawing.Point(160, 184)
  94.         Me.PictureBox1.Name = "PictureBox1"
  95.         Me.PictureBox1.Size = New System.Drawing.Size(64, 48)
  96.         Me.PictureBox1.TabIndex = 6
  97.         Me.PictureBox1.TabStop = False
  98.         '
  99.         'btnLoad
  100.         '
  101.         Me.btnLoad.Location = New System.Drawing.Point(16, 184)
  102.         Me.btnLoad.Name = "btnLoad"
  103.         Me.btnLoad.Size = New System.Drawing.Size(120, 48)
  104.         Me.btnLoad.TabIndex = 5
  105.         Me.btnLoad.Text = "Load Cursor from File"
  106.         '
  107.         'btnHide
  108.         '
  109.         Me.btnHide.Location = New System.Drawing.Point(16, 128)
  110.         Me.btnHide.Name = "btnHide"
  111.         Me.btnHide.Size = New System.Drawing.Size(120, 48)
  112.         Me.btnHide.TabIndex = 5
  113.         Me.btnHide.Text = "Hide Cursor"
  114.         '
  115.         'CursorForm
  116.         '
  117.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  118.         Me.ClientSize = New System.Drawing.Size(480, 269)
  119.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1, Me.btnLoad, Me.btnHide, Me.btnSetCursor, Me.chkClipCursor, Me.btnMoveCursor, Me.lblStatus})
  120.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  121.         Me.Name = "CursorForm"
  122.         Me.Text = "CursorForm"
  123.         Me.ResumeLayout(False)
  124.  
  125.     End Sub
  126.  
  127. #End Region
  128.  
  129.     ' move the cursor to the center of the form
  130.  
  131.     Private Sub btnMoveCursor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveCursor.Click
  132.         With Me.ClientRectangle
  133.             Cursor.Position = Me.PointToScreen(New Point(CInt(.Width / 2), CInt(.Height / 2)))
  134.         End With
  135.     End Sub
  136.  
  137.     ' clip the cursor to the client area of the ClipCursor checkbox control
  138.  
  139.     Private Sub chkClipCursor_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkClipCursor.CheckedChanged
  140.         If chkClipCursor.Checked Then
  141.             ' clip the mouse cursor
  142.             Cursor.Clip = chkClipCursor.RectangleToScreen(chkClipCursor.ClientRectangle)
  143.         Else
  144.             ' free the mouse cursor
  145.             Cursor.Clip = Nothing
  146.         End If
  147.     End Sub
  148.  
  149.     ' this event fires when the UI is idle, so we can use it to
  150.     ' display mouse coordinates
  151.  
  152.     Private Sub Application_Idle(ByVal sender As System.Object, ByVal e As System.EventArgs)
  153.         lblStatus.Text = "Mouse Position = " & Me.PointToClient(Cursor.Position).ToString
  154.     End Sub
  155.  
  156.     ' changes the shape of the cursor
  157.  
  158.     Private Sub btnSetCursor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetCursor.Click
  159.         If btnSetCursor.Text = "Show Hourglass" Then
  160.             'Cursor.Current = Cursors.WaitCursor
  161.             Me.Cursor = Cursors.WaitCursor
  162.             btnSetCursor.Text = "Show Default"
  163.         Else
  164.             'Cursor.Current = Cursors.Default
  165.             Me.Cursor = Cursors.Default
  166.             btnSetCursor.Text = "Show Hourglass"
  167.         End If
  168.     End Sub
  169.  
  170.     ' hide and show the cursor
  171.  
  172.     Private Sub btnHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHide.Click
  173.         If btnHide.Text = "Hide Cursor" Then
  174.             Cursor.Hide()
  175.             btnHide.Text = "Show Cursor"
  176.         Else
  177.             Cursor.Show()
  178.             btnHide.Text = "Hide Cursor"
  179.         End If
  180.     End Sub
  181.  
  182.     ' load a cursor from file
  183.  
  184.     Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
  185.         ' Ask for a file name, exit if canceled
  186.         OpenFileDialog1.Filter = "Cursor files|*.cur"
  187.         If OpenFileDialog1.ShowDialog() = DialogResult.Cancel Then Exit Sub
  188.  
  189.         Dim c As Cursor
  190.         Try
  191.             ' load the cursor
  192.             c = New Cursor(OpenFileDialog1.FileName)
  193.         Catch ex As Exception
  194.             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  195.             Exit Sub
  196.         End Try
  197.  
  198.         ' display the cursor on the picturebox
  199.         Dim gr As Graphics = PictureBox1.CreateGraphics
  200.         gr.Clear(Color.White)
  201.         c.DrawStretched(gr, PictureBox1.ClientRectangle)
  202.         gr.Dispose()
  203.  
  204.         Me.Cursor = c
  205.     End Sub
  206.  
  207. End Class
  208.